//
//  MKMapView-Zoom.swift
//  ZhiYunWeather
//
//  Created by Wade on 2024/7/8.
//

import Foundation
import MapKit
extension MKMapView {
    var zoomLevel: Int {
        get{
            return Int(log2(360 * Double(frame.size.width) / 256.0 / region.span.longitudeDelta))
        }
       
        set{
            setZoomlevel(zoomLevel: newValue, animation: false)
        }
    }
    
    func setZoomlevel(zoomLevel:Int){
        setZoomlevel(zoomLevel: zoomLevel, animation: false)
    }
    
    func setZoomlevel(zoomLevel:Int,animation:Bool){
        // MKCoordinateSpanMake(0,360 / Double(pow(2, zoomLevel)) * Double(self.frame.size.width) / 256);
        NSLog("setZoomlevel:"+String(zoomLevel))
        let long = 360.0 / Double(pow(2, Double(zoomLevel)) * Double(self.frame.size.width) / 256.0);
        let span = MKCoordinateSpan(latitudeDelta: 0, longitudeDelta: long)
        self.setRegion(MKCoordinateRegion(center: self.centerCoordinate, span: span), animated: animation)
    }
    
}

